home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
listings
/
v_01_07
/
1n07011b
< prev
next >
Wrap
Text File
|
1990-11-03
|
819b
|
33 lines
//
// comma_long function: convert long to ASCII,
// with commas every three digits and sign preservation:
//
#include <stdio.h>
#include <math.h> // needed for labs()
void comma_long(long number, char *string);
void main() // test calls to comma_long() function
{
char string[100];
comma_long(123456L, string);
printf("result 1: %s\n", string);
comma_long(-2323435L, string);
printf("result 2: %s\n", string);
}
void comma_long(long number, char *string)
{
if (labs(number) >= 1000) {
comma_long(number/1000, string);
sprintf(string + strlen(string), ",%03ld",
labs(number) % 1000);
}
else
sprintf(string, "%ld", number);
}